package com.yanqu.road.server.cmd.activity.yqhotel;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.YqHotelProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;

import com.yanqu.road.server.gameplayer.module.activity.yqhotel.YqHotelModule;
import com.yanqu.road.server.manger.activity.yqhotel.YqHotelMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Cmd(code = GameProtocol.S_YQ_HOTEL_MANUAL, desc = "阳泉酒家获取手册信息")
public class YqHotelGetManualCmd extends AbstractCommand {

    private int activityId;

    private Map<Integer,Integer> titleMap = new ConcurrentHashMap<>();

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        YqHotelProto.YqHotelManualReqMsg reqMsg = YqHotelProto.YqHotelManualReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(YqHotelMgr.getActivityTimeAcId() != activityId){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        titleMap = player.getModule(YqHotelModule.class).getTitleManualMap();
    }

    @Override
    public void sendData(GamePlayer player) {
        YqHotelProto.YqHotelManualRespMsg.Builder respMsg = YqHotelProto.YqHotelManualRespMsg.newBuilder();
        respMsg.setRet(ret);
        if(ret == 0){
            for(int titleId : titleMap.keySet()){
                YqHotelProto.YqHotelManualTemp.Builder manualTempMsg = YqHotelProto.YqHotelManualTemp.newBuilder();
                manualTempMsg.setTitleId(titleId);
                manualTempMsg.setCount(titleMap.get(titleId));
                respMsg.addManuals(manualTempMsg);
            }
        }
        player.sendPacket(ClientProtocol.U_YQ_HOTEL_MANUAL, respMsg);
    }
}
